<template>
	<view class="user">
		我的
	</view>
</template>

<script>
import { APP_ID} from '@/config/app';
import Cache from 'utils/cache';
import store from "store";

	export default{
		data(){
			return{
				
			}
		},
		onLoad(){
			//虚拟用户登录
			// this.mnlogin();
		},
		onShow(){
			this.userinit();
			console.log("cs",this.$store.state.app.isLogin)
			this.state = this.$store.state.app.isLogin;
			if(this.state){
				this.getnewUserInfo();
			}
		},
		methods:{
			isJSON(str) {
			    if (typeof str == 'string') {
			        try {
			            var obj = JSON.parse(str);
			            if (typeof obj == 'object' && obj) {
			                return true;
			            } else {
			                return false;
			            }
			
			        } catch (e) {
			            console.log('error：' + str + '!!!' + e);
			            return false;
			        }
			    }
			},
			
			mnlogin(){
				let opt = {
					token:'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJyZWN5Y2xlIiwiZXhwIjoxNjUzNTY3NzYzLCJhdWQiOiJtZW1iZXIiLCJuYmYiOjE2NTI5NjI5NjMsImlhdCI6MTY1Mjk2Mjk2MywibWVtYmVyX2lkIjo1NTZ9.ijj0IX_L1G2NzlNHZOsqUccIULUtwIAH6pKuEsN6v-8',
					// token:'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJyZWN5Y2xlIiwiZXhwIjoxNjQ2NjUxNzA2LCJhdWQiOiJtZW1iZXIiLCJuYmYiOjE2NDYwNDY5MDYsImlhdCI6MTY0NjA0NjkwNiwibWVtYmVyX2lkIjo1MDV9.nTfTJYrlopeeerEOdt7BUwt3NICAYFxTlTczWvDnr7Y',
					time:604800
					// time:100
				}
				this.$store.commit('LOGIN',opt);
			},
			userinit(){
				let code = this.getUrlCode('code')
				if (code) {
					if(!this.$store.state.app.isLogin){
						this.getOpenidAndUserinfo(code);
					}
				}
				
				if(this.$store.state.app.isLogin){
					this.state = this.$store.state.app.isLogin;
					this.isLogin = true;
					if(this.isJSON(this.$store.state.app.userInfo)){
						this.userInfo = JSON.parse(this.$store.state.app.userInfo);
					}else{
						this.userInfo = this.$store.state.app.userInfo;
					}
					if(this.userInfo.types == 2){
						this.toolData[0].flag = true
					}else if(this.userInfo.types == 3){
						this.toolData[1].flag = true
					}
				}else{
					this.isLogin = false;
					this.userInfo = {};
				}
			},
			// 获取code
			getCode() {
				// 判断是微信浏览器
				let isWechat = String(navigator.userAgent.toLowerCase().match(/MicroMessenger/i)) === "micromessenger";
				if (isWechat) {
					// 截取地址中的code，如果没有code就去微信授权，如果已经获取到code了就直接把code传给后台获取openId
					let code = this.getUrlCode('code');
					if (code === null || code === '') {
						let appid = APP_ID;
						let local = encodeURIComponent("https://xuejisoft.2017mac.cn/h5/pages/user/user");
						// console.log(window.location.href)
						// return;
						window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid + '&redirect_uri=' + local + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';
					}
				}
			},
			// 解析获取coude
			getUrlCode (name) {
				return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ''])[1].replace(/\+/g, '%20')) || null
			},
			// 请求登录
			getOpenidAndUserinfo(code) {
				let that = this;
				let data = {
					code
				};
				if(uni.getStorageSync('teacher_id')){
					let teacher_id = uni.getStorageSync('teacher_id');
					data.teacher_id = teacher_id;
				}
				login(data).then(res => {
					that.userInfo = res.data;
					that.isLogin = true;
					let opt = {
						token:res.data.token,
						time:604800
						// time:100
					}
					store.commit('LOGIN',opt);
					store.commit('UPDATE_USERINFO',res.data);
					// that.state = that.$store.state.app.isLogin;
					window.location.href='/h5/pages/user/user';
				})
			}
		}
	}
</script>

<style lang="less" scoped>
.user{
	
}
</style>
